using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using Core.Utils;
using UnityEngine;

public abstract class ListComponent<Data,View> : MonoBehaviour where View : MonoBehaviour {
    private List<Data> mDataList;

    private View mTemplateView;
    private void Awake() {
        mDataList = new List<Data>();
        mTemplateView = transform.GetChild(0).GetComponent<View>();
        mTemplateView.gameObject.SetActive(false);
        PoolMgr.Ins.CreatePool(mTemplateView.gameObject);
    }

    protected abstract void updateView(Data data, View view, int index);
    
    public void RefreshData(List<Data> dataList) {
        for (int i = 0;  i < transform.childCount;) {
            PoolMgr.Ins.Release(transform.GetChild(i).gameObject);
        }

        mDataList = dataList;
        for (int i = 0; i < dataList.Count; ++i) {
            View view = PoolMgr.Ins.Copy(mTemplateView.gameObject).GetComponent<View>();
            view.transform.SetParent(transform);
            view.gameObject.SetActive(true);
            updateView(mDataList[i],view,i);
        }
    }
}
